package vn.com.atm;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.library.UserFunctions;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SearchActivity extends Activity{
	
	private Spinner spnBank;
	private Button btnViewMap,btnSearch, btnCancel;
	private EditText edAtmCode, edAddress;
	private static final String TAG_BANK = "bank";
	private static final String TAG_BANK_NAME = "bankName";
	private static final String TAG_BANK_CODE = "bankCode", TAG_ATM_CODE = "atmCode", TAG_ADDRESS = "address";
	String bankCode, atmCode, address;
	JSONArray arrBank;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		 UserFunctions userFunction = new UserFunctions();
	     JSONObject json = userFunction.listBank();
	     
	   
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_search);
		
		spnBank = (Spinner)findViewById(R.id.spnBank);
		
		
		 //Create array
        //ArrayAdapter<CharSequence> adapBank = ArrayAdapter.createFromResource(SearchActivity.this, R.array.list_bank, android.R.layout.simple_spinner_item);
		ArrayAdapter<CharSequence> adapBank = new ArrayAdapter<CharSequence>(SearchActivity.this, android.R.layout.simple_spinner_item); 
        adapBank.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //set adapter
        
        spnBank.setAdapter(adapBank);
        //Set selected Listener
        
        try {
			if(json.getString(TAG_BANK)!= null)
			 {
				arrBank = json.getJSONArray(TAG_BANK);
				for(int i = 0; i < arrBank.length(); i ++)
				{
					JSONObject ojbBank = arrBank.getJSONObject(i);
					adapBank.add(ojbBank.getString(TAG_BANK_NAME));
				}
			 }
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      
        spnBank.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adap, View v,
					int position, long arg3) {
				
				bankCode = adap.getItemAtPosition(position).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        //Button view map
        
        btnViewMap = (Button)findViewById(R.id.btnViewMap);
        
        btnViewMap.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent i = new Intent(SearchActivity.this,MapViewActivity.class);
				startActivity(i);
				
			}
		});
        
        // Button search
        
        btnSearch = (Button)findViewById(R.id.btnSearch);
        edAtmCode = (EditText)findViewById(R.id.edAtmCode);
        edAddress = (EditText)findViewById(R.id.edAddress);
        btnSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				address = edAddress.getText().toString();
				atmCode = edAtmCode.getText().toString();
				
				 // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                		AllATMActivity.class);
                // sending pid to next activity
                in.putExtra(TAG_BANK_CODE, bankCode);
                in.putExtra(TAG_ATM_CODE, atmCode);
                in.putExtra(TAG_ADDRESS, address);
                
                startActivity(in);
                finish();
			}
		});
        
        // Button Cancel
        btnCancel = (Button)findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getBaseContext(),MainActivity.class);
				startActivity(i);
				
				finish();
			}
		});
        
	}
	
	
	

}
